﻿using BZhanHengjicvWPF.Common;
using BZhanHengjicvWPF.Common.Models;
using BZhanHengjicvWPF.Extensions;
using Prism.Commands;
using Prism.Ioc;
using Prism.Mvvm;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BZhanHengjicvWPF.ViewModels
{
    public class MainViewModel:BindableBase,IConfigureService
    {
        private readonly IRegionManager regionManager;
        private readonly IContainerProvider containerProvider;
        private IRegionNavigationJournal journal;
        public MainViewModel(IRegionManager regionManager,IContainerProvider containerProvider)
        {
            //NavigateCommand = new DelegateCommand<MenuBar>(Navigate);
            UserName = AppSession.UserName;
            this.regionManager = regionManager;
            this.containerProvider = containerProvider;
        }
        private ObservableCollection<MenuBar> menuBars=new ObservableCollection<MenuBar>();

		public ObservableCollection<MenuBar> MenuBars
        {
			get { return menuBars; }
			set { menuBars = value;  RaisePropertyChanged(); }
		}

        private string userName;

        public string UserName
        {
            get { return userName; }
            set { userName = value;RaisePropertyChanged(); }
        }

        public DelegateCommand<MenuBar> NavigateCommand    { get { return new DelegateCommand<MenuBar>(Navigate); } }
        public DelegateCommand GoBackCommand
        {
            get
            {
                return new DelegateCommand(() =>
                {
                    if(journal!=null&& journal.CanGoBack) journal.GoBack();
                });
            }
        }
        public DelegateCommand GoForwardCommand
        {
            get
            {
                return new DelegateCommand(() =>
                {
                    if (journal != null&&journal.CanGoForward) journal.GoForward();
                });
            }
        }

        public DelegateCommand LogOutCommand
        {
            get
            {
                return new DelegateCommand(() =>
                {
                    //注销当前用户
                    App.LogOut(containerProvider);
                });
            }
        }



        private void Navigate(MenuBar obj)
        {
            if (obj.Icon == null || string.IsNullOrWhiteSpace(obj.NameSpace))
                return;
                regionManager.Regions[PrismManager.MainViewRegionName].RequestNavigate(obj.NameSpace, back =>
                {
                    journal = back.Context.NavigationService.Journal;//这里就是导航的上一步和下一步，导航日志
                });
        }

        void CreatMenus() 
        {
            MenuBars.Add(new MenuBar() { Icon = "Home", Title = "首页", NameSpace = "IndexView" });
            MenuBars.Add(new MenuBar() { Icon = "Notebook", Title = "代办事项", NameSpace = "ToDoView" });
            MenuBars.Add(new MenuBar() { Icon = "NotebookEditOutline", Title = "备忘录", NameSpace = "MemoView" });
            MenuBars.Add(new MenuBar() { Icon = "Cog", Title = "设置", NameSpace = "SettingsView" });
        }

        public void Configure()
        {
            userName = AppSession.UserName;
            CreatMenus();
            regionManager.Regions[PrismManager.MainViewRegionName].RequestNavigate("IndexView");
        }
    }
}
